package com.zzb.generator;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class CodeGenerator {
    public static void main(String[] args) {
        List<String> tables = new ArrayList<>();
//        tables.add("blog");
//        tables.add("blog_category");
//        tables.add("collect");
//        tables.add("comment");
//        tables.add("download");
//        tables.add("follow");
        tables.add("invite");
//        tables.add("likes");
//        tables.add("question");
//        tables.add("question_category");
//        tables.add("say");
        tables.add("user");
        tables.add("user_donate");
        tables.add("user_info");
        tables.add("user_role");

        FastAutoGenerator.create("jdbc:mysql://localhost:3306/forum?useUnicode=true&characterEncoding=utf-8&useSSL=false","root","123456")
                .globalConfig(builder -> {
                    builder.author("张智博")              //作者
                            .outputDir(System.getProperty("user.dir")+"\\src\\main\\java")    //输出路径(写到java目录)
                            .enableSwagger()             //开启swagger
                            .disableOpenDir()            //禁止执行完后资源管理器弹出
                            .dateType(DateType.ONLY_DATE)//定义生成的实体类中日期类型
                            .commentDate("yyyy-MM-dd");
//                            .fileOverride()             //开启覆盖之前生成的文件

                })
                .packageConfig(builder -> {
                    builder.parent("com")
                            .moduleName("zzb")
                            .entity("pojo")
                            .service("service")
                            .serviceImpl("service.impl")
                            .controller("controller")
                            .mapper("mapper")
                            .xml("mapper.xml")
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml,System.getProperty("user.dir")+"\\src\\main\\resources\\mapper"));
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tables)
                            //service
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            //pojo
                            .entityBuilder()
                            .naming(NamingStrategy.underline_to_camel)
                            .columnNaming(NamingStrategy.underline_to_camel)
                            .enableLombok()
                            .enableChainModel()
                            .idType(IdType.ASSIGN_UUID)
                            .logicDeleteColumnName("deleted")
                            .enableTableFieldAnnotation()
                            //controller
                            .controllerBuilder()
                            .formatFileName("%sController")
                            .enableRestStyle()
                            .enableHyphenStyle()
                            //mapper
                            .mapperBuilder()
                            .enableBaseResultMap()  //生成通用的resultMap
                            .superClass(BaseMapper.class)
                            .formatMapperFileName("%sMapper")
                            .enableMapperAnnotation()
                            .formatXmlFileName("%sMapper");
                })
                .execute();
    }
}